package com.jh.base;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.jh.base.basefragment.NoNetFrameLayout;
import com.jh.base.present.BaseIPresent;
import com.jh.base.view.BaseView;

public abstract class BaseActivity extends AppCompatActivity {
    public String TAG = getClass().getSimpleName();
    private FrameLayout mContentView;
    private ViewGroup mLlContentToolbar, mLlContent, mMainView;
    private NoNetFrameLayout mNo_net_content;
    private LayoutInflater mIntlater;

    private BaseView mBaseView;
    private BaseIPresent mBasePresent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        View inflate = LayoutInflater.from(this).inflate(R.layout.activity_base, null, false);
        mContentView.addView(inflate, 0);
        initUI();
    }

    private View getContentView() {
        mContentView = new FrameLayout(this);
        return mContentView;
    }

    /**
     * @Author :Mr.JH
     * @Date :2022/4/7 10:22
     * @Description: 初始化UI
     */
    private void initUI() {
        installCustomTool();
        //头部布局
        mLlContentToolbar = findViewById(R.id.ll_content_toolbar);
        //主布局
        mLlContent = findViewById(R.id.ll_content);
        //网络出错提示
        mNo_net_content = findViewById(R.id.no_net_content);
        mIntlater = LayoutInflater.from(this);
        //设置头部
        if (installToolBarCenter() != null) {
            setContentToolBar(installToolBarCenter());
        }
        //设置主界面
        setContent(contentLayoutId());
        //加载控件
        initComponent();
    }

    //加载控件
    protected void initComponent() {
        mBaseView = installView();
        mBasePresent = installPresent(mBaseView);
//        if (mBaseView != null) {
//            mBaseView.setPresent(mBasePresent);
//        }
    }

    protected abstract BaseIPresent installPresent(BaseView baseView);

    protected abstract BaseView installView();

    protected void installCustomTool() {
    }

    /**
     * @Author :Mr.JH
     * @Date :2022/4/7 10:47
     * @Description: 初始化头部
     */
    protected View installToolBarCenter() {
        return null;
    }

    /**
     * @param view
     * @Author :Mr.JH
     * @Date :2022/4/7 10:47
     * @Description: 添加头部内容
     */
    public void setContentToolBar(View view) {
        mLlContentToolbar.removeAllViews();
        mLlContentToolbar.addView(view);
    }

    /**
     * @param id 布局id
     * @Author :Mr.JH
     * @Date :2022/4/7 10:49
     * @Description: 添加内容
     */
    public void setContent(Integer id) {
        mMainView = (ViewGroup) mIntlater.inflate(id, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        mContentView.addView(mMainView, lp);
    }

    /**
     * @Author :Mr.JH
     * @Date :2022/4/7 10:52
     * @Description: 获取界面
     */
    protected abstract Integer contentLayoutId();

    public ViewGroup getMainView() {
        return mMainView;
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.i(TAG, "onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.i(TAG, "onRestart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtils.i(TAG, "onPause");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtils.i(TAG, "onResume");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtils.i(TAG, "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtils.i(TAG, "onDestroy");
    }
}